package com.action;



import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import sun.awt.image.codec.JPEGImageEncoderImpl;

import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.utils.ImageUtil;
@ParentPackage("json-default")
public class ImageAction extends BaseAction{
	private InputStream imageStream;
	public InputStream getImageStream() {
		return imageStream;
	}
	public void setImageStream(InputStream imageStream) {
		this.imageStream = imageStream;
	}
	@Action(value = "code", results = { @Result(type = "stream", params={"inputName","imageStream"},name = "success") })
	public String execute() {
		Map<String, BufferedImage> map = ImageUtil.createImage();
		String code = map.keySet().iterator().next();
		sessionMap.put("yzm", code);
		System.out.println("code is :"+code);
		BufferedImage image = map.get(code);
		try {
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			JPEGImageEncoder encoder = new JPEGImageEncoderImpl(out);
			encoder.encode(image);
			byte[] buf = out.toByteArray();
			
			imageStream = new ByteArrayInputStream(buf);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "success";
	}

}
